/*
 * Copyright 2010 WithOne Co. Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jp.co.withone.osgi.gadget.upnp;

import java.util.ArrayList;
import java.util.List;


import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

public class ContainerHandler implements ContentHandler {

    private Contents contents;
    private List<Container> conList;
    private Container con;
    private List<Item> itemList;
    private Item item;

    private String tagName;

    /**
     * 要素の開始タグ読み込み時
     */
    public void startElement(String uri, String localName, String qName, Attributes attributes) {
        if ("DIDL-Lite".equals(qName)) {
            this.contents = new Contents();
        }

        if ("container".equals(qName)) {
            if (this.conList == null) {
                this.conList = new ArrayList<Container>();
            }
            this.con = new Container();

            // IDの取得
            int index = attributes.getIndex("id");
            String id = attributes.getValue(index);
            int end = id.lastIndexOf('/');
            if (end > 0) {
                id = id.substring(end + 1);
            }
            this.con.id = id;

            index = attributes.getIndex("childCount");
            String childCount = attributes.getValue(index);
            this.con.childCound = Integer.parseInt(childCount);
        }

        if (this.contents != null) {
            this.tagName = qName;
        }

        if ("item".equals(qName)) {
            if (this.itemList == null) {
                this.itemList = new ArrayList<Item>();
            }
            this.item = new Item();
        }
    }

    /**
     * テキストデータ読み込み時
     */
    public void characters(char[] ch, int offset, int length) {
        if (this.con == null && this.item == null) {
            return;
        }

        if (this.con != null && "dc:title".equals(this.tagName)) {
            this.con.title = new String(ch, offset, length);
        }

        if (this.item != null && "dc:title".equals(this.tagName)) {
            this.item.title = new String(ch, offset, length);
        }
        if (this.item != null && "res".equals(this.tagName)) {
            this.item.url = new String(ch, offset, length);
        }
    }

    /**
     * 要素の終了タグ読み込み時
     */
    public void endElement(String uri, String localName, String qName) {
        if ("container".equals(qName)) {
            this.conList.add(this.con);
            this.con = null;
        }
        if ("item".equals(qName)) {
            this.itemList.add(this.item);
            this.item = null;
        }

        if ("DIDL-Lite".equals(qName)) {
            if (this.conList != null) {
                this.contents.containeis = this.conList.toArray(new Container[this.conList.size()]);
                this.conList = null;
            }
            if (this.itemList != null) {
                this.contents.items = this.itemList.toArray(new Item[this.itemList.size()]);
                this.itemList = null;
            }
        }
    }

    public Contents getContents() {
        return this.contents;
    }

    public void clear() {
        this.contents = null;
    }

    public void endDocument() throws SAXException {
        // 空実装
    }

    public void endPrefixMapping(String prefix) throws SAXException {
        // 空実装
    }

    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
        // 空実装
    }

    public void processingInstruction(String target, String data) throws SAXException {
        // 空実装
    }

    public void setDocumentLocator(Locator locator) {
        // 空実装
    }

    public void skippedEntity(String name) throws SAXException {
        // 空実装
    }

    public void startDocument() throws SAXException {
        // 空実装
    }

    public void startPrefixMapping(String prefix, String uri) throws SAXException {
        // 空実装
    }

}
